﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;

namespace Interface.Users
{
    public partial class UserList : System.Web.UI.Page
    {
        DataConnection dc = new DataConnection();
        protected override void OnInit(EventArgs e)
        {
            gridUsers.PageIndexChanging += new GridViewPageEventHandler(gridUsers_PageIndexChanging);           
            gridUsers.SelectedIndexChanging += new GridViewSelectEventHandler(gridUsers_SelectedIndexChanging);
        }

        void gridUsers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int id = (int)gridUsers.DataKeys[e.NewSelectedIndex].Value;
            Response.Redirect("UserDetails.aspx?ID=" + id);
            //Response.Redirect("UserDetails.aspx?ID=" + ((Button)gridUsers.Rows[e.NewSelectedIndex].FindControl("btnSelect")).CommandArgument.ToString());            
        }

        void gridUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridUsers.PageIndex = e.NewPageIndex;
            loadUser();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["AdminSession"] == null)
            {

                Response.Redirect("~/Home.aspx");
            }
            else
            {
                loadUser();
            }
        }

        private string ResolveURL(string p)
        {
            throw new NotImplementedException();
        }

        protected void loadUser()
        {
            string sqlStr = "Select * from Users";
            DataTable dt = dc.getData(sqlStr);
            DataTable dtUser = dataTableInitiate();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int stt = i + 1;
                    DataRow row = dt.Rows[i];
                    int ID = Convert.ToInt32(row["UserID"].ToString());
                    string userName = row["Username"].ToString();
                    string fullName = row["FullName"].ToString();
                    bool bgender = Boolean.Parse(row["Gender"].ToString());
                    string strGender = "";
                    if (bgender == true)
                        strGender = "Nam";
                    else
                        strGender = "Nữ";
                    string userType = row["MemberType"].ToString();
                    DataRow dtRow = dtUser.NewRow();
                    dtRow["ID"] = ID;
                    dtRow["STT"] = stt;
                    dtRow["Username"] = userName;
                    dtRow["FullName"] = fullName;
                    dtRow["Gender"] = strGender;
                    dtRow["MemberType"] = userType;
                    dtUser.Rows.Add(dtRow);
                }
            }
            gridUsers.DataSource = dtUser;
            gridUsers.DataBind();
        }

        protected DataTable dataTableInitiate()
        {
            DataTable dt = new DataTable();
            DataColumn col = dt.Columns.Add("ID", typeof(int));
            col.AutoIncrement = true;
            col.AutoIncrementStep = 1;
            col.AutoIncrementSeed = 1;
            dt.Columns.Add("STT", typeof(string));
            dt.Columns.Add("Username", typeof(string));
            dt.Columns.Add("FullName", typeof(string));
            dt.Columns.Add("Gender", typeof(string));
            dt.Columns.Add("MemberType", typeof(string));           
            return dt;
        }







        
    }
}